package com.hangim.pce.controller;

import com.hangim.pce.common.web.PceBaseController;
import com.hangim.pce.model.SysDict;
import com.hangim.pce.service.SysDictService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.github.pig.common.constant.CommonConstant;
import com.github.pig.common.util.Query;
import com.github.pig.common.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 字典表 前端控制器
 * </p>
 *
 * @author hongjiliang
 * @since 2018-10-19
 */
@RestController
@RequestMapping("/sysDict")
public class SysDictController extends PceBaseController {
    @Autowired
    private SysDictService sysDictService;

    /**
     * 通过ID查询
     *
     * @param id ID
     * @return SysDict
     */
    @GetMapping("/{id}")
    public R<SysDict> get(@PathVariable Long id) {
        return new R<>(sysDictService.selectById(id));
    }


    /**
     * 分页查询信息
     *
     * @param params 分页对象
     * @return 分页对象
     */
    @RequestMapping("/page")
    public Page page(@RequestParam Map<String, Object> params) {
        params.put(CommonConstant.DEL_FLAG, CommonConstant.STATUS_NORMAL);
        return sysDictService.selectPage(new Query<>(params), new EntityWrapper<SysDict>().like("name", (String) params.get("name")).orderBy(true, "created_at").orderBy(true, "updated_at"));
    }

    /**
     * 添加
     *
     * @param sysDict 实体
     * @return success/false
     */
    @PostMapping
    public R<Integer> add(@RequestBody SysDict sysDict) {
        sysDict.setCreatedAt(new Date());
        sysDict.setCreatedBy(getCurUserId());
        return new R<>(sysDictService.insert(sysDict));
    }

    /**
     * 删除
     *
     * @param id ID
     * @return success/false
     */
    @DeleteMapping("/{id}")
    public R<Integer> delete(@PathVariable Long id) {
        SysDict sysDict = new SysDict();
        sysDict.setId(id);
        sysDict.setUpdatedAt(new Date());
        sysDict.setUpdatedBy(getCurUserId());
        sysDict.setActive(false);
        return new R<>(sysDictService.updateById(sysDict));
    }

    /**
     * 编辑
     *
     * @param sysDict 实体
     * @return success/false
     */
    @PutMapping
    public R<Integer> edit(@RequestBody SysDict sysDict) {
        sysDict.setUpdatedAt(new Date());
        sysDict.setUpdatedBy(getCurUserId());
        return new R<>(sysDictService.updateById(sysDict));
    }
}
